package com.open.common.task;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.w3c.dom.Document;

import javax.xml.xpath.XPathConstants;
import java.util.HashMap;

public class HuaDaoPost {

    public static String getToken() {
        HashMap paramMap = new HashMap();
        paramMap.put("AppID", "D1086495W0F66W4E74WB91DWB601B77530E3");
        paramMap.put("AppSecret", "DA47CFEFL2F6FL471ALAB37L1E5C527733AA");
        paramMap.put("Key", "A542C14BHF745H4FA4HA6C4HE0DC751A0BA1");
        //链式构建请求
        String tokenXmlJson = HttpRequest.post("http://opensdk.emay.cn:9080/HD_GetAccess_Token.asmx/GetACCESS_TOKEN")
                .header(Header.USER_AGENT, "opensdk.emay.cn")//头信息，多个头信息多次调用此方法即可
                .header(Header.HOST, "opensdk.emay.cn")//头信息，多个头信息多次调用此方法即可
                .form(paramMap)//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        Console.log(tokenXmlJson);

        Document docResult = XmlUtil.parseXml(tokenXmlJson);
        Object value = XmlUtil.getByXPath("//string", docResult, XPathConstants.STRING);
        if (String.valueOf(value).contains("errcode")) {
            return value.toString();
        }
        JSONObject jsonObject = JSONUtil.parseObj(String.valueOf(value));
        Object access_token = jsonObject.get("access_token");
        return access_token.toString();
    }

    public static String getEmda_xdzh_dt(HashMap paramMap) {
        /*
        HashMap paramMap = new HashMap();
        paramMap.put("Phone","18033915729");
        paramMap.put("cycle","24");
        paramMap.put("Platform","0");
        paramMap.put("ACCESS_TOKEN","12CEC5003523455FA49DFED29B4E1967432600352120CE347C485FB631538AA9E53898574A7540B28D3AE642C4231918");
        */
        //链式构建请求
        String result = HttpRequest.post("http://opensdk.emay.cn:9080/MADE_API/emda_xdzh.asmx/GetEmda_xdzh_dt")
                .header(Header.USER_AGENT, "opensdk.emay.cn")//头信息，多个头信息多次调用此方法即可
                .header(Header.HOST, "opensdk.emay.cn")//头信息，多个头信息多次调用此方法即可
                .form(paramMap)//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        Console.log(result);

        Document docResult = XmlUtil.parseXml(result);
        Object value = XmlUtil.getByXPath("//string", docResult, XPathConstants.STRING);
        return String.valueOf(value);
    }

    public static void main(String[] args) {
        /*String temp ="{ \"CODE\": \"200\", \"PHONE\": \"1890215****\", \"PROVINCE\": \"北京\", \"CITY\": \"北京\", \"RESULTS\": [\n" +
                "{ \"TYPE\": \"EMR002\", \"CYCLE\": \"2015-07-11--2016-07-11\", \"DATA\": [\n" +
                "{ \"P_TYPE\": \"2\", \"PLATFORMCODE\": \"EM_111604\", \"REGISTERTIME\": \"2016/3/14 0:00:00\"\n" +
                "},{ \"P_TYPE\": \"1\", \"PLATFORMCODE\": \"EM_111605\", \"REGISTERTIME\": \"2016/3/14 0:00:00\"\n" +
                "},{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_111602\", \"REGISTERTIME\": \"2016/3/14 0:00:00\"\n" +
                "}\n" +
                "]\n" +
                "},{ \"TYPE\": \"EMR004\", \"CYCLE\": \"2015-07-11--2016-07-11\", \"DATA\": [\n" +
                "{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_106744\", \"APPLICATIONTIME\": \"2016/3/14 0:00:00\", \"APPLICATIONAMOUNT\": \"0W～0.2W\", \"APPLICATIONRESULT\": \"yes\"\n" +
                "},{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_106745\", \"APPLICATIONTIME\": \"2016/3/14 0:00:00\", \"APPLICATIONAMOUNT\": \"0W～0.2W\", \"APPLICATIONRESULT\": \"yes\"\n" +
                "},{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_106746\", \"APPLICATIONTIME\": \"2016/3/14 0:00:00\", \"APPLICATIONAMOUNT\": \"0W～0.2W\", \"APPLICATIONRESULT\": \"yes\"\n" +
                "}\n" +
                "]\n" +
                "},{ \"TYPE\": \"EMR007\", \"CYCLE\": \"2015-07-11--2016-07-11\", \"DATA\": [\n" +
                "{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_111604\", \"LOANLENDERSTIME\": \"2016/3/14 0:00:00\", \"LOANLENDERSAMOUNT\": \"0W～0.2W\"\n" +
                "},{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_111603\", \"LOANLENDERSTIME\": \"2016/3/14 0:00:00\", \"LOANLENDERSAMOUNT\": \"0W～0.2W\"\n" +
                "},{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_111602\", \"LOANLENDERSTIME\": \"2016/3/14 0:00:00\", \"LOANLENDERSAMOUNT\": \"0W～0.2W\"\n" +
                "}\n" +
                "]\n" +
                "},{ \"TYPE\": \"EMR009\", \"CYCLE\": \"2015-07-11--2016-07-11\", \"DATA\": [\n" +
                "{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_121900\", \"REJECTIONTIME\": \"2016/3/14 0:00:00\"\n" +
                "},{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_121901\", \"REJECTIONTIME\": \"2016/3/14 0:00:00\"\n" +
                "},{ \"P_TYPE\": \"0\", \"PLATFORMCODE\": \"EM_121902\", \"REJECTIONTIME\": \"2016/3/14 0:00:00\"\n" +
                "}\n" +
                "]\n" +
                "},{ \"TYPE\": \"EMR012\", \"CYCLE\": \"2015-07-11--2016-07-11\", \"DATA\": [\n" +
                "{ \"PLATFORM\": \"EM_121900\", \"COUNTS\": \"284\", \"MONEY\": \"0W～0.2W\", \"D_TIME\": \"2016-04-10\"\n" +
                "},{ \"PLATFORM\": \"EM_106744\", \"COUNTS\": \"12\", \"MONEY\": \"0W～0.2W\",\n" +
                "\"D_TIME\": \"2016-04-10\"\n" +
                "},{ \"PLATFORM\": \"EM_121905\", \"COUNTS\": \"60\", \"MONEY\": \"0W～0.2W\", \"D_TIME\": \"2016-04-10\"\n" +
                "}\n" +
                "]\n" +
                "},{ \"TYPE\": \"EMR013\", \"CYCLE\": \"2015-07-11--2016-07-11\", \"DATA\": [\n" +
                "{ \"PLATFORM\": \"EM_111602\", \"MONEY\": \"0W～0.2W\"\n" +
                "}\n" +
                "]\n" +
                "}\n" +
                "]\n" +
                "}";

        System.out.println(JSONUtil.parseObj(temp));


        JSONObject jsonObject = JSONUtil.parseObj(temp);

        System.out.println(jsonObject.get("CODE"));
        System.out.println(jsonObject.get("PHONE"));
        System.out.println(jsonObject.get("PROVINCE"));
        System.out.println(jsonObject.get("CITY"));

        JSONArray results = (JSONArray)jsonObject.get("RESULTS");


        for (int i = 0; i < results.size(); i++) {

            System.out.println(results.get(i));
            JSONObject jsonObject1 = JSONUtil.parseObj(results.get(i).toString());

            ArrayList<HashMap<String,String>> arrayList_EMR002 = new ArrayList<HashMap<String,String>>();
            if("EMR002".equals(jsonObject1.get("TYPE"))){
                //JSONObject jsonObject_EMR002 = JSONUtil.parseObj(jsonObject1.get("DATA").toString());
                JSONArray jsonArray_EMR002 = JSONUtil.parseArray(jsonObject1.get("DATA").toString());
                for (int j = 0; j < jsonArray_EMR002.size(); j++) {
                    arrayList_EMR002.add((HashMap<String,String>)jsonArray_EMR002.get(j));
                }
            }

        }*/
        getToken();
    }
}
